package util;

import util.Node.NodeState;

public class FieldCreator {
	
	public static Node[][] createEmpty() {
		Node[][] nodeNet = new Node[PublicVars.getFieldWidth()][PublicVars.getFieldHeight()];
		for (int i = 0; i < nodeNet.length; i++) {
			for (int j = 0; j < nodeNet[i].length; j++) {
				nodeNet[i][j] = new Node(i, j);
			}
		}
		nodeNet[0][0].setStart(true);
		nodeNet[PublicVars.getFieldWidth()-1][PublicVars.getFieldHeight()-1].setTarget(true);
		PublicVars.setZoom(580.0f/(PublicVars.getFieldHeight()*PublicVars.getNodeSize()));
		Pathfinder.getMainframe().getPanelSettings().refresh();
		return nodeNet;
	}
	
	public static Node[][] createRandomMaze(int percent) {
		Node[][] nodeNet = new Node[PublicVars.getFieldWidth()][PublicVars.getFieldHeight()];
		for (int i = 0; i < nodeNet.length; i++) {
			for (int j = 0; j < nodeNet[i].length; j++) {
				nodeNet[i][j] = new Node(i, j);
				if(Math.random()*100 < percent){
					nodeNet[i][j].setObstacle(true);;
				}
			}
		}
		nodeNet[0][0].setObstacle(false);
		nodeNet[0][0].setStart(true);
		nodeNet[PublicVars.getFieldWidth()-1][PublicVars.getFieldHeight()-1].setObstacle(false);
		nodeNet[PublicVars.getFieldWidth()-1][PublicVars.getFieldHeight()-1].setTarget(true);
		PublicVars.setZoom(580.0f/(PublicVars.getFieldHeight()*PublicVars.getNodeSize()));
		Pathfinder.getMainframe().getPanelSettings().refresh();
		return nodeNet;
	}
	
	public static Node[][] createLogicMaze(int percentFirst, int percentNext) {
		return createLogicMazeInternal(percentFirst, percentNext);
	}
	
	public static Node[][] createLogicMaze(int percent) {
		return createLogicMazeInternal(percent/2, percent);
	}
	
	private static Node[][] createLogicMazeInternal(int percentFirst, int percentNext) {
		Node[][] nodeNet = new Node[PublicVars.getFieldWidth()][PublicVars.getFieldHeight()];
		int percent = percentFirst;
		for (int i = 0; i < nodeNet.length; i++) {
			for (int j = 0; j < nodeNet[i].length; j++) {
				boolean hasNext = false;
				
				if(i == 0){
					hasNext = true;
				} else if(i+1 == nodeNet.length){
					hasNext = true;
				} else if(nodeNet[i-1][j].isObstacle()){
					hasNext = true;
				}
				if(j == 0){
					hasNext = true;
				} else if(j+1 == nodeNet[i].length){
					hasNext = true;
				} else if(nodeNet[i][j-1].isObstacle()){
					hasNext = true;
				}
				
				
				if(hasNext){
					percent = percentNext;
				} else {
					percent = percentFirst;
				}
				nodeNet[i][j] = new Node(i, j);
				if(Math.random()*100 < percent){
					nodeNet[i][j].setObstacle(true);;
				}
			}
		}
		nodeNet[0][0].setObstacle(false);
		nodeNet[0][0].setStart(true);
		nodeNet[PublicVars.getFieldWidth()-1][PublicVars.getFieldHeight()-1].setObstacle(false);
		nodeNet[PublicVars.getFieldWidth()-1][PublicVars.getFieldHeight()-1].setTarget(true);
		PublicVars.setZoom(580.0f/(PublicVars.getFieldHeight()*PublicVars.getNodeSize()));
		Pathfinder.getMainframe().getPanelSettings().refresh();
		return nodeNet;
	}
	
	public static void clearNodeNet(){
		for (int i = 0; i < PublicVars.getFieldWidth(); i++) {
			for (int j = 0; j < PublicVars.getFieldHeight(); j++) {
				PublicVars.getNodeNet()[i][j].setCosts(0);
				PublicVars.getNodeNet()[i][j].setgCosts(0);
				PublicVars.getNodeNet()[i][j].sethCosts(0);
				PublicVars.getNodeNet()[i][j].setState(NodeState.UNKNOWN);
				PublicVars.getNodeNet()[i][j].setLast(null);
			}
		}
	}
}
